// pages/setting/editPassword.js
const httpUtils = require('../../utils/httpUtils')
const ui = require('../../utils/ui')

Page({
  data: {
    userPhone: '',
    userPassword: '',
    codeButton: '发送验证码',
    codeButtonEnable: false,
    codeSecond: 60, 
    userLogin: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (wx.getStorageSync('USERDATA')) {
      this.setData({
        userPhone: wx.getStorageSync('USERDATA').mobile,
        userLogin: true
      })
    }
  },
  //点击修改密码
  actionEditPassword(event) {
    if (event.detail.value.phone.length == 0) {
      ui.showToast('请输入手机号');
      return;
    }
    if (event.detail.value.password.length == 0) {
      ui.showToast('请输入新密码');
      return;
    }
    if (event.detail.value.code.length == 0) {
      ui.showToast('请输入验证码');
      return;
    }
    let obj = {
      method: 'POST',
      contentType: 'application/x-www-form-urlencoded',
      url: '/user/sms-code',
      showLoading: true,
      data: {
        mobile: event.detail.value.phone,
        verifyCode: event.detail.value.code
      }
    }
    httpUtils.request(obj).then(res => {

      let obj2 = {
        method: 'PUT',
        url: '/user/'+wx.getStorageSync('USERDATA').platformId +'/password',
        showLoading: true,
        data: {
          newPassword: event.detail.value.password
        }
      }
      httpUtils.request(obj2).then(res => {
        wx.showToast({
          title: '修改成功',
          icon: 'success',
          duration: 2000,
          mask: true,
          success: function() {
            setTimeout(function() {
              wx.navigateBack({
                delta: 0,
              });
            }, 1500)
          }
        });
      }).catch(err => {
        console.log('ERROR')
      });

    }).catch(err => {
      console.log('ERROR')
    });
  },
  //发送验证码
  sendCode() {
    var that = this;
    if (this.data.userPhone.length == 0) {
      ui.showToast('请输入手机号');
      return;
    }
    let obj = {
      method: 'GET',
      contentType: 'application/x-www-form-urlencoded',
      url: '/user/' + this.data.userPhone + '/sms',
      showLoading: true,
      data: {}
    }
    httpUtils.request(obj).then(res => {
      that.setData({
        codeButtonEnable: true
      })
      that.timer()
    }).catch(err => {
      console.log('ERROR')
    });

  },
  //倒计时
  timer: function () {
    let promise = new Promise((resolve, reject) => {
      let setTimer = setInterval(
        () => {
          this.setData({
            codeSecond: this.data.codeSecond - 1
          })
          if (this.data.codeSecond <= 0) {
            this.setData({
              codeSecond: 60,
              codeButtonEnable: false,
              codeButton: '重新发送'
            })
            resolve(setTimer)
          } else {
            this.setData({
              codeButton: this.data.codeSecond + 's'
            })
          }
        }, 1000)
    })
    promise.then((setTimer) => {
      clearInterval(setTimer)
    })
  },
})